package com.dingding.action;

import org.apache.log4j.Logger;

import com.dingding.bean.databean.push.PushCompetitiveOrder;
import com.dingding.bean.databean.response.DataResponse;
import com.dingding.common.core.beans.Userordert;
import com.dingding.common.core.util.ToolUtil;

/**
 * 
 * @ClassName: AlipayRequestAction
 * @Description: 支付宝调用接口
 * @author lijinchao
 * @date 2015-11-10 下午03:17:13
 * @version
 */
@SuppressWarnings("all")
public class AlipayRequestAction extends BaseAction {

	private static final Logger log = Logger
			.getLogger(AlipayRequestAction.class);
	/**
	 * 本类唯一标识
	 */
	private static final long serialVersionUID = 3051642533915274264L;

	private String buyer_email; // 买家支付宝账户
	private Double total_fee; // 交易金额
	private String trade_status; // 交易状态
	private String body; // 商品描述
	private String out_trade_no; // 账户操作ID

	public String passengerPay() {

		log.info("----------支付宝支付成功，回调服务器-------------");
		
		log.info("支付回调参数：" + this.toString());
		
		//判断支付宝回调服务器时，传入的参数是否为空，如果为空，则返回null
		if(ToolUtil.isBlank(trade_status) || ToolUtil.isBlank(out_trade_no)){
			log.info("支付宝回调服务器时，传入的参数为空！！！");
			return null;
		}
		
		// 判断支付宝交易是否成功
		if (trade_status.equals("TRADE_SUCCESS")) {
			boolean row = false;

			//车主抢单推送
			PushCompetitiveOrder pushCompetitiveOrder = null;
			
			// 根据订单Id获取订单信息
			Userordert userordert = super.userOrderService.getUserordert(
					out_trade_no, new DataResponse());

			if(userordert == null){
				log.error("根据支付宝回调返回的订单信息为空！ 订单Id为：" + out_trade_no + " 类：" + this.getClass()+" line: 58");
			}
			
			pushCompetitiveOrder = super.userOrderService.passengerDebit(userordert,pushCompetitiveOrder,
					new DataResponse());
			if (pushCompetitiveOrder != null) {

				try {
					// 乘客Id
					String passengerId = null;
					// 司机Id
					String driverId = null;

					// 乘客支付成功，向司机发送短信
					// 判断该订单是否为空
					if (userordert != null) {

						if (userordert.getOrderType() == 2) { // 当下单用户为乘客时

							// 乘客Id
							passengerId = userordert.getPlaceOrderUserId();
							// 司机Id
							driverId = userordert.getGrabOrderUserId();

						} else if (userordert.getOrderType() == 1) {// 当下单用户为司机时

							// 乘客Id
							passengerId = userordert.getGrabOrderUserId();
							// 司机Id
							driverId = userordert.getPlaceOrderUserId();
						}

						//发送邮件
						boolean flag = super.userOrderService.sendEmail(pushCompetitiveOrder,userordert);
						
						//邮件发送成功，打印success
						if(flag){
							// 打印success，并关闭流
							returnSuccess();
						}
						// 发送短信以及推送消息
						super.userOrderService.passengerPaySendInfo(
								passengerId, driverId, userordert);
						
						log.info("支付支付，回调成功");
						
					}

				} catch (Exception e) {
					log.error("支付宝回调失败！", e);
				}
			}
		}
		// 判断是否为交易创建
		else if (trade_status.equals("WAIT_BUYER_PAY")) {
			returnSuccess();

		}
		// 判断是否为交易成功
		else if (trade_status.equals("TRADE_FINISHED")) {
			returnSuccess();

		}
		// 判断是否为交易关闭
		else if (trade_status.equals("TRADE_CLOSED")) {
			returnSuccess();

		}

		return null;
	}

	/**
	 * @Title: returnSuccess
	 * @Description: 打印success，并关闭流
	 * @return void
	 * @throws
	 */
	private void returnSuccess() {
		super.getWriter().print("success");
		super.getWriter().flush();
		super.getWriter().close();
	}

	public String getBuyer_email() {
		return buyer_email;
	}

	public void setBuyer_email(String buyer_email) {
		this.buyer_email = buyer_email;
	}

	public Double getTotal_fee() {
		return total_fee;
	}

	public void setTotal_fee(Double total_fee) {
		this.total_fee = total_fee;
	}

	public String getTrade_status() {
		return trade_status;
	}

	public void setTrade_status(String trade_status) {
		this.trade_status = trade_status;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getOut_trade_no() {
		return out_trade_no;
	}

	public void setOut_trade_no(String out_trade_no) {
		this.out_trade_no = out_trade_no;
	}

	@Override
	public String toString() {
		return "AlipayRequestAction [buyer_email=" + buyer_email + ", total_fee=" + total_fee + ", trade_status="
				+ trade_status + ", body=" + body + ", out_trade_no=" + out_trade_no + "]";
	}
	
	
}
